<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Badge
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Badge_IndexController extends Core_Controller_Action_Standard {
	protected $_navigation;
	
	public function init() {
		if (! $this->_helper->requireAuth ()->setAuthParams ( 'badge', null, 'view' )->isValid ())
			return;
		
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			if (0 !== ($badge_id = ( int ) $this->_getParam ( 'badge_id' )) && null !== ($badge = Engine_Api::_ ()->getItem ( 'badge', $badge_id ))) {
				Engine_Api::_ ()->core ()->setSubject ( $badge );
			}
		}
		
		$this->_helper->requireUser->addActionRequires ( array ()

		 );
		
		$this->_helper->requireSubject->setActionRequireTypes ( array ('view' => 'badge' ) );
	}
	
	public function indexAction() {
		$this->_helper->content->setNoRender ()->setEnabled ();
	}
	
	// NONE USER SPECIFIC METHODS
	public function browseAction() {
		// Get navigation
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'badge_main' );
		
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->view->form = $form = new Badge_Form_Filter_Badge ();
		
		$values = array ();
		// Populate form data
		if ($form->isValid ( $this->_getAllParams () )) {
			$values = $form->getValues ();
		}
		
		$values = Engine_Api::_ ()->getApi ( 'filter', 'radcodes' )->removeKeyEmptyValues ( $values );
		$this->view->formValues = $values;
		
		$this->view->assign ( $values );
		
		$values ['limit'] = ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'badge.badgeperpage', 10 );
		$values ['search'] = 1;
		$this->view->paginator = $paginator = Engine_Api::_ ()->badge ()->getBadgesPaginator ( $values );
		$paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
		
		$this->view->notablemember = ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'badge.notablemember', 0 );
	
	}
	
	public function viewAction() {
		$this->view->viewer = $viewer = Engine_Api::_ ()->user ()->getViewer ();
		$this->view->badge = $badge = Engine_Api::_ ()->core ()->getSubject ( 'badge' );
		
		$badge->view_count ++;
		$badge->save ();
		
		$params = array ('limit' => ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'badge.memberperpage', 10 ), 'page' => $this->_getParam ( 'page', 1 ) );
		
		if ($badge->isMembershipTypeAward ()) {
			$params = array_merge ( $params, array ('active' => true ) );
			
			$this->view->awardPaginator = $awardPaginator = $badge->getAwardsPaginator ( $params );
			
			$this->view->canRequest = ($badge->award_member_request == 2 || ($badge->award_member_request == 1 && $badge->testMemberRequirements ( $viewer ))) && ! $badge->isAwardedMember ( $viewer, true );
		
		} else if ($badge->isMembershipTypeMapping ()) {
			$this->view->memberPaginator = $memberPaginator = $badge->getMembersPaginator ( $params );
		}
	
	}

}

